fix: normalize cross-provider tool call ids - #327
Merged
Conversation
🦋 Changeset detectedLatest commit: 228628e The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
Merged
5 tasks
dv8128-agent Bot
pushed a commit
to dv8128/kimi-code
that referenced
this pull request
Aug 1, 2026
Kimi models are trained to see tool-call ids echoed back in conversation history in the canonical shape `functions.<name>:<idx>`. The cross-provider id normalizer added in MoonshotAI#327 sanitizes these ids (`functions.Read:0` -> `functions_Read_0`), after which Kimi emits reasoning with no tool call and stops the turn (`APIEmptyResponseError` / `finishReason=stop`). Kimi served through an OpenAI-compatible endpoint (litellm / vLLM / SGLang / Azure) is configured as an `openai` provider, so the fix must cover the OpenAI chat-completions and responses policies, not just the native Kimi policy: that is the path in the linked issue, whose session dump carries the canonical id `functions.Skill:0` in history. The failure is masked on the official endpoint (which normalizes server-side) but reproducible on self-hosted deployments, where the history id format is the model's only cue. Add `sanitizeToolCallIdPreservingNative` / `sanitizeOpenAIResponsesCallIdPreservingNative`: a canonical native id is passed through verbatim, everything else keeps the existing normalization. Wire them into the OpenAI chat, OpenAI responses, and Kimi tool-call-id policies in both the default (kosong) and agent-core-v2 engines. The carve-out is keyed on the id shape, so it applies to any `openai` provider (Kimi behind such an endpoint does not self-identify as Kimi at the policy layer, so the shape is the only signal). Trade-off: a Kimi-authored id replayed onto a different, stricter OpenAI-compatible backend is now passed through rather than sanitized; a canonical id is nonetheless a valid free-form tool_call id under the OpenAI chat-completions spec. Charset-restricted providers (Anthropic, `^[a-zA-Z0-9_-]+$`) keep sanitizing, so MoonshotAI#327's Kimi -> Anthropic replay fix is intact; Google (function names, not ids) is unaffected. Resolve MoonshotAI#520. Refs: Moonshot Kimi-K2 tool_call_guidance.md; vLLM blog "Chasing 100% Accuracy: Debugging Kimi K2's Tool-Calling on vLLM". Acked-by: Andrey Akinshin <andrey.akinshin@jetbrains.com>
5 tasks
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issue
No linked issue. This fixes provider switching failures observed when historical tool call IDs from one provider are invalid for another provider.
Problem
Some providers emit tool call IDs that are valid for their own protocol but rejected by another provider during session replay. For example, Kimi can emit IDs like
Write:6, while Anthropic requires tool IDs to match^[a-zA-Z0-9_-]+$, causing restored sessions to fail after switching providers.What changed
Added a shared provider-boundary tool call ID normalizer for
kosongand wired it into Anthropic, Kimi, OpenAI Chat Completions, and OpenAI Responses. The normalizer keeps valid IDs stable, rewrites invalid IDs only in outbound provider payloads, and preserves tool call/result matching with deterministic collision handling.Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.Verification:
pnpm exec vitest run --config vitest.config.tsinpackages/kosongpnpm --filter @moonshot-ai/kosong run typecheck